package gb;

import gb.ord.LexOrd;
import gb.struct.Monomial;
import gb.struct.Polynomial;

public class Parser {
    
    private static Polynomial current;

    /**
     * parses a polynomial encoded in a string
     * <br>
     * a polynomial has form: <br>
     * 
     * ( [sign]literal [^ power] )*
     * 
     * @param toParse the string to parse
     * @return a polynomial object
     * @throws Exception if string is not a polynomial or cannot be parsed
     */
    public static Polynomial parse(String toParse) throws Exception {
        Polynomial temp = getPoly();
        int coef = 1, power, i = 0;
        String variable;
        
        toParse = toParse.trim();
        
        if (toParse.charAt(i) == '-' ) {
            coef = -1;
        }
        if (Character.isDigit(toParse.charAt(i))) {
            Character.digit(toParse.charAt(i), coef);
        } else {
            throw new Exception("Cannot parse string!");
        }
        
        while (Character.isDigit(toParse.charAt(i))) {
            int t = 0;
            Character.digit(toParse.charAt(i), t);
            coef = coef * 10 + t;
        }
        Monomial toAdd = new Monomial(coef);        
        
        
        
        return null;
    }
    
    public static Polynomial getPoly() {
        if (current == null) {
            current = new Polynomial(new LexOrd());
        }
        
        return current;
    }
}
